package binarySortTree;

public class BinarySortTree {
    public static void main(String[] args) {
        int[] arr = {7, 3, 10, 12, 5, 1, 9, 2};
        BinarySortTreeDemo binarySortTreeDemo = new BinarySortTreeDemo();
        for (int i = 0; i < arr.length; i++) {
            binarySortTreeDemo.add(new Node(arr[i]));
        }
        binarySortTreeDemo.infixOrder();


        binarySortTreeDemo.deleteNode(7);
        binarySortTreeDemo.deleteNode(3);
        binarySortTreeDemo.deleteNode(12);
        binarySortTreeDemo.deleteNode(5);
        binarySortTreeDemo.deleteNode(9);
        binarySortTreeDemo.deleteNode(2);
        binarySortTreeDemo.deleteNode(10);
        binarySortTreeDemo.deleteNode(1);
        System.out.println("删除后");
        binarySortTreeDemo.infixOrder();
    }
}

class BinarySortTreeDemo {
    private Node root;


    /**
     * 删除节点的最小节点
     */
    public int deleteTreeMin(Node node) {
        if (node == null) {
            return -1;
        }
        Node target = node;
        while (target.left != null) {
            target = target.left;
        }
        deleteNode(target.value);
        return target.value;
    }

    /**
     * 删除节点
     *
     * @param value
     */
    public void deleteNode(int value) {
        if (root == null) {
            return;
        }
        Node target = search(value);
        if (target == null) {
            return;
        }
        //父节点可能为null
        if (root.left == null && root.right == null) {
            //说明只有root一个节点
            root = null;
            return;
        }
        //找到target的父节点
        Node parent = searchParent(value);
        //判断要删除节点是不是叶子节点
        if (target.left == null && target.right == null) {
            if (parent.left == target) {
                parent.left = null;
            } else {
                parent.right = null;
            }
        } else if (target.left != null && target.right != null) {//删除的节点有两个子节点
            int minValue = deleteTreeMin(target.right);
            if (minValue == -1) {
                return;
            }
            target.value = minValue;

        } else {//删除的节点只有一个子节点
            if (parent != null) {
                if (target == parent.left) {
                    if (target.left != null) {
                        parent.left = target.left;
                    } else {
                        parent.left = target.right;
                    }
                } else {
                    if (target.left != null) {
                        parent.right = target.left;
                    } else {
                        parent.right = target.right;
                    }
                }
            } else {
                if (target.left != null) {
                    root = target.left;
                } else {
                    root = target.right;
                }
            }

        }
    }


    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }
    }

    public Node search(int value) {
        if (root == null) {
            return null;
        } else {
            return root.search(value);
        }
    }

    public Node searchParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }

    public void infixOrder() {
        if (root == null) {
            System.out.println("二叉树为空");
        } else {
            root.infixOrder();
        }
    }
}

class Node {
    int value;
    Node left;
    Node right;

    public Node(int value) {
        this.value = value;
    }

    /**
     * 查找节点
     *
     * @param value
     * @return
     */
    public Node search(int value) {
        if (value == this.value) {
            return this;
        } else if (value < this.value) {
            if (this.left == null) {
                return null;
            }
            return this.left.search(value);
        } else {
            if (this.right == null) {
                return null;
            }
            return this.right.search(value);
        }
    }

    /**
     * 查找要找的节点的父节点
     *
     * @param value
     * @return
     */
    public Node searchParent(int value) {
        if ((this.left != null && this.left.value == value) ||
                (this.right != null && this.right.value == value)) {
            return this;
        } else {
            if (value < this.value && this.left != null) {
                return this.left.searchParent(value);
            } else if (value >= this.value && this.right != null) {
                return this.right.searchParent(value);
            } else {
                return null;
            }
        }
    }

    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    public void add(Node node) {
        if (node == null) {
            return;
        }
        if (node.value < this.value) {
            if (this.left == null) {
                this.left = node;
            } else {
                this.left.add(node);
            }
        } else {
            if (this.right == null) {
                this.right = node;
            } else {
                this.right.add(node);
            }
        }
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                '}';
    }
}
